package com.foreknow.service.impl;

import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GuestbookServiceImpl implements GuestbookService {
    @Override
    public boolean save(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl service = new GuestbookDaoImpl();
        service.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = service.add(guestbook);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    public static void main(String[] args) {
        GuestbookService service = new GuestbookServiceImpl();

        Guestbook gb = new Guestbook();
        gb.setName("fender");
        gb.setPhone("12121212121");
        gb.setEmail("zjjlive123@163.com");
        gb.setTitle("test......");
        gb.setContent("fender cs");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String createdTime = simpleDateFormat.format(date);
        gb.setCreatedTime(createdTime);

        boolean isRight = service.save(gb);
        System.out.println(isRight);
    }
}
